import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cart:[],
  },
  mutations: {
    addtocart(state,goodslist){

      var status = false;
       state.cart.some(item => {
         if(item.id == goodslist.id){
           item.count +=goodslist.count;
           status = true;
           return true;
            }
          })
       if(status == false){
         console.log(goodslist)
         state.cart.push(goodslist);
       }
       
       console.log(state.cart);
       localStorage.setItem('cart',JSON.stringify(state.cart));

    },
    gettocart(state,tocart){
      state.cart = tocart;
    }

  },
  getters:{
    getcount(state){
      var sum = 0;
      state.cart.forEach(item =>{
        sum += item.count;
      })
      return sum;
    }
  },
  actions: {
  },
  modules: {
  }
})
